//
//  TagSelectionViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/8/2.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "TagSelectionViewController.h"
#import "Tool.h"


@interface TagSelectionViewController ()

@property(nonatomic,strong)NSArray <TagSelection *>*dataArr;
@property(nonatomic,assign)BOOL multiSelect;
@property(nonatomic,copy)SelectData selectData;

@end

@implementation TagSelectionViewController

- (instancetype)initWithData:(NSArray <TagSelection *>*)datas multiSelect:(BOOL)multiSelect selectData:(SelectData)selectData
{
    self = [super initWithStyle:(UITableViewStyleGrouped)];
    if (self) {
        self.dataArr = datas;
        self.multiSelect = multiSelect;
        self.selectData = selectData;
        
        if (multiSelect) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)rightAction
{
    if (self.selectData) {
        NSMutableArray *arr = [NSMutableArray array];
        for (TagSelection* model in self.dataArr) {
            if (model.select_tagSelection) {
                [arr addObject:model];
            }
        }
        self.selectData(arr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellName = @"TagSelectionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    TagSelection* model = self.dataArr[indexPath.row];
    
    cell.textLabel.text = model.name;
    if (model.select_tagSelection) {
        cell.imageView.image = [UIImage imageNamed:@"ClassificationChoice"];
    }else{
        cell.imageView.image = [UIImage imageNamed:@"ClassificationNotChoice"];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TagSelection* model = self.dataArr[indexPath.row];
    if (self.multiSelect) {
        model.select_tagSelection = !model.select_tagSelection;
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (model.select_tagSelection) {
            cell.imageView.image = [UIImage imageNamed:@"ClassificationChoice"];
        }else{
            cell.imageView.image = [UIImage imageNamed:@"ClassificationNotChoice"];
        }
    }else{
        if (self.selectData) {
            self.selectData(@[model]);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
